//
//  StashCell.swift
//  Box
//
//  Created by mn on 2025/3/28.
//
import UIKit
import SnapKit

class StashCell: UICollectionViewCell {
    
    private let imageView = UIImageView()
    private let badgeImageView = UIImageView()
    
    private let sourceView = SourceView(text: "赠送", color: .red)

    private let titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = .black
        label.numberOfLines = 2
        label.textAlignment = .left
        return label
    }()
    
    private let timeLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 10)
        label.textColor = .gray
        label.textAlignment = .left
        return label
    }()
    
//    private let buttonStackView: UIStackView = {
//        let stackView = UIStackView()
//        stackView.axis = .horizontal
//        stackView.distribution = .fillEqually
//        stackView.spacing = 10
//        return stackView
//    }()
    
    private let priceLabel: PaddingLabel = {
        let label = PaddingLabel()
        label.font = UIFont.boldSystemFont(ofSize: 9)
        label.textColor = .white
        label.backgroundColor = .fromHex("#232321")
        label.layer.cornerRadius = 4
        label.layer.masksToBounds = true
        label.textAlignment = .center
        label.text = "￥7499.00"
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupViews() {
        contentView.backgroundColor = .white
        contentView.layer.cornerRadius = 8
        contentView.layer.masksToBounds = true
        contentView.addSubview(imageView)
        contentView.addSubview(titleLabel)
        contentView.addSubview(timeLabel)
//        contentView.addSubview(buttonStackView)
        imageView.addSubview(badgeImageView)
//        imageView.addSubview(sourceView)
        imageView.addSubview(priceLabel)

//        sourceView.snp.makeConstraints { make in
//            make.top.left.equalToSuperview().offset(8)
//            make.height.equalTo(18)
//        }
        
        imageView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(imageView.snp.width).multipliedBy(0.75)
        }
        badgeImageView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(9)
            make.right.equalToSuperview().offset(-6)
        }
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(imageView.snp.bottom).offset(2)
            make.left.right.equalToSuperview().inset(10)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
            make.left.right.equalToSuperview().inset(10)
        }
        
//        buttonStackView.snp.makeConstraints { make in
//            make.top.equalTo(timeLabel.snp.bottom).offset(8)
//            make.left.right.bottom.equalToSuperview().inset(10)
//        }
        
        priceLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-8)
            make.bottom.equalToSuperview().offset(-8)
            make.height.equalTo(18)
        }
        
//        for _ in 0..<1 {
//            let button = UIButton(type: .system)
//            button.setTitle("提货", for: .normal)
//            buttonStackView.addArrangedSubview(button)
//        }
        
        let button = UIButton(type: .custom)
        button.setTitle("提货", for: .normal)
        button.backgroundColor  = .black
        button.layer.cornerRadius = 5
        button.titleLabel?.font = .systemFont(ofSize: 14,weight: .medium)
        button.setTitleColor(.white, for: .normal)
        contentView.addSubview(button)
        button.snp.makeConstraints { make in
//            make.top.equalTo(timeLabel.snp.bottom).offset(8)
            make.bottom.equalToSuperview().offset(-10)
            make.right.equalToSuperview().offset(-8)
            make.size.equalTo(CGSize(width: 43, height: 30))
        }
        button.addAction { [weak self] in
            let vc = ConfirmDeliveryViewController()
            vc.stashModel = self?.stashModel
            self?.findViewController()?.navigationController?.push(vc)
        }
        
        titleLabel.text = "苹果 Apple iPhone 14系列pro/Promax 移动联通电..."
        timeLabel.text = "2022-09-31 16:12:40"
    }
    func bage(index: Int) {
        switch index%3 {
        case 0:
            badgeImageView.image = UIImage(named: "bronze")
        case 1:
            badgeImageView.image = UIImage(named: "gold")
        case 2:
            badgeImageView.image = UIImage(named: "platinum")
        default:
            badgeImageView.image = UIImage(named: "diamond")
        }
    }
    var stashModel: StashModel?{
        didSet{
            guard let stashModel else { return }
            titleLabel.text = stashModel.goodsName
            imageView.kf.setImage(with: URL(string: stashModel.imgUrlList.first ?? ""))
            timeLabel.text = stashModel.createTime.YMDHMS()
            priceLabel.text = String(format: "¥%.2f", Float(stashModel.price) * 0.01)
        }
    }
}
